#Binary parser template

import os, sys
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class temp(Model3D):
    '''Replace 'temp' with a suitable name of your choice. Try to follow
    the standard: GameName_FormatExt'''
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(temp,self).__init__("XAC")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def read_name(self):
        
        return self.inFile.read_string(self.inFile.read_short())
        
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        self.inFile.read_short()
        header = self.inFile.read_string(6)
        
        meshOffset = self.inFile.read_long()
        #self.inFile.read_long(15)
        #self.read_name()
        #self.inFile.read_long(19)
        self.inFile.seek(meshOffset)
        pieces = self.read_name()
        self.inFile.read_long(7)
        meshName = self.read_name()
        self.create_object(meshName)
        print meshName, self.inFile.tell()
        
        numFace = self.inFile.read_long()
        print numFace
        for i in xrange(numFace):
            self.inFile.read_float(2)
            v1 = self.inFile.read_short()
            
            self.inFile.read_float(2)
            v2 = self.inFile.read_short()
            
            self.inFile.read_float(2)
            v3 = self.inFile.read_short()
            
            self.create_face(meshName, i)
            self.add_face_verts(meshName, i, [v1, v2, v3])
            
        numVerts = self.inFile.read_long()
        for i in xrange(numVerts):
            self.inFile.read_long(2)
            coords = self.inFile.read_float(3)
            self.inFile.read_long()
            normals = self.inFile.read_float(3)
            self.inFile.read_float(3)
            
            self.create_vertex(meshName, i)
            self.add_coords(meshName, i, coords)
            self.add_vert_normals(meshName, i, normals)
            
        
            
        
def read_file(path):
    '''Read the file'''
    
    openFile = StructReader(open(path, 'rb'))
    obj = temp(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "\x06\x00Header", "ABC", "Might and Magic IX ABC"

if __name__ == '__main__':
    
    file1 = ""
    obj = read_file(file1)